package com.umeox.iqibla.custom_tasbih.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.TextView
import androidx.core.graphics.toColorInt

class FinishTaskActivity : ThemeAppCompatActivity() {

    companion object {
        private const val INTENT_KEY_TASK_GOAL_SIZE = "goalSize"

        fun intoFinishTaskActivity(context: Context, goalSize: Int) {
            context.startActivity(Intent(context, FinishTaskActivity::class.java).apply {
                putExtra(INTENT_KEY_TASK_GOAL_SIZE, goalSize)
            })
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_finish_task)

        val goalSize = intent?.getIntExtra(INTENT_KEY_TASK_GOAL_SIZE, 0)!!
        val tvContent = findViewById<TextView>(R.id.tv_content)

        tvContent.text = setPartialTextColor(
            String.format(getString(R.string.k2_tasbih_custom_times), goalSize),
            "$goalSize",
            "#FDC95A".toColorInt()
        )
    }

    private fun setPartialTextColor(fullStr: String, colorStr: String, color: Int): CharSequence {
        if (fullStr.contains(colorStr)) {
            val codeShowStrSpannableStr = SpannableString(fullStr)
            val amStrEndIndex = fullStr.indexOf(colorStr) + colorStr.length
            codeShowStrSpannableStr.setSpan(
                ForegroundColorSpan(color),
                fullStr.indexOf(colorStr),
                amStrEndIndex,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            codeShowStrSpannableStr.setSpan(
                AbsoluteSizeSpan(sp2px(20f), false),
                fullStr.indexOf(colorStr),
                amStrEndIndex,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            return codeShowStrSpannableStr
        } else {
            return fullStr
        }
        return ""
    }

    private fun sp2px(spValue: Float): Int {
        val fontScale: Float =
            getResources().displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }

}